package com.qiuyuliantu.guide.service.impl;

import java.util.List;
import com.qiuyuliantu.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.qiuyuliantu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.qiuyuliantu.guide.domain.GuideCookbookMethod;
import com.qiuyuliantu.guide.mapper.GuideCookbookClassMapper;
import com.qiuyuliantu.guide.domain.GuideCookbookClass;
import com.qiuyuliantu.guide.service.IGuideCookbookClassService;

/**
 * 美食菜谱分类Service业务层处理
 *
 * @author lmx
 * @date 2025-03-24
 */
@Service
public class GuideCookbookClassServiceImpl implements IGuideCookbookClassService
{
    @Autowired
    private GuideCookbookClassMapper guideCookbookClassMapper;

    /**
     * 查询美食菜谱分类
     *
     * @param id 美食菜谱分类主键
     * @return 美食菜谱分类
     */
    @Override
    public GuideCookbookClass selectGuideCookbookClassById(Long id)
    {
        GuideCookbookClass guideCookbookClass = guideCookbookClassMapper.selectGuideCookbookClassById(id);
        guideCookbookClass.setGuideCookbookMethodList(guideCookbookClassMapper.selectGuideCookbookMethodList(guideCookbookClass.getClassName()));
        return guideCookbookClass;
    }

    /**
     * 查询美食菜谱分类列表
     *
     * @param guideCookbookClass 美食菜谱分类
     * @return 美食菜谱分类
     */
    @Override
    public List<GuideCookbookClass> selectGuideCookbookClassList(GuideCookbookClass guideCookbookClass)
    {
        return guideCookbookClassMapper.selectGuideCookbookClassList(guideCookbookClass);
    }

    /**
     * 新增美食菜谱分类
     *
     * @param guideCookbookClass 美食菜谱分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGuideCookbookClass(GuideCookbookClass guideCookbookClass)
    {
        guideCookbookClass.setCreateTime(DateUtils.getNowDate());
        int rows = guideCookbookClassMapper.insertGuideCookbookClass(guideCookbookClass);
        insertGuideCookbookMethod(guideCookbookClass);
        return rows;
    }

    /**
     * 修改美食菜谱分类
     *
     * @param guideCookbookClass 美食菜谱分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGuideCookbookClass(GuideCookbookClass guideCookbookClass)
    {
        guideCookbookClass.setUpdateTime(DateUtils.getNowDate());
        guideCookbookClassMapper.deleteGuideCookbookMethodByClassName(guideCookbookClass.getId());
        insertGuideCookbookMethod(guideCookbookClass);
        return guideCookbookClassMapper.updateGuideCookbookClass(guideCookbookClass);
    }

    /**
     * 批量删除美食菜谱分类
     *
     * @param ids 需要删除的美食菜谱分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGuideCookbookClassByIds(Long[] ids)
    {
        guideCookbookClassMapper.deleteGuideCookbookMethodByClassNames(ids);
        return guideCookbookClassMapper.deleteGuideCookbookClassByIds(ids);
    }

    /**
     * 删除美食菜谱分类信息
     *
     * @param id 美食菜谱分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGuideCookbookClassById(Long id)
    {
        guideCookbookClassMapper.deleteGuideCookbookMethodByClassName(id);
        return guideCookbookClassMapper.deleteGuideCookbookClassById(id);
    }

    /**
     * 新增美食菜谱教程信息
     *
     * @param guideCookbookClass 美食菜谱分类对象
     */
    public void insertGuideCookbookMethod(GuideCookbookClass guideCookbookClass)
    {
        List<GuideCookbookMethod> guideCookbookMethodList = guideCookbookClass.getGuideCookbookMethodList();
        Long id = guideCookbookClass.getId();
        if (StringUtils.isNotNull(guideCookbookMethodList))
        {
            List<GuideCookbookMethod> list = new ArrayList<GuideCookbookMethod>();
            for (GuideCookbookMethod guideCookbookMethod : guideCookbookMethodList)
            {
                guideCookbookMethod.setClassName(guideCookbookClass.getClassName());
                list.add(guideCookbookMethod);
            }
            if (list.size() > 0)
            {
                guideCookbookClassMapper.batchGuideCookbookMethod(list);
            }
        }
    }
}
